package org.springframework.samples.petclinic.web

import java.text.SimpleDateFormat
import java.util.Date

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.propertyeditors.CustomDateEditor
import org.springframework.beans.propertyeditors.StringTrimmerEditor
import org.springframework.web.bind.WebDataBinder
import org.springframework.web.bind.support.WebBindingInitializer
import org.springframework.web.context.request.WebRequest

import org.springframework.samples.petclinic.Clinic
import org.springframework.samples.petclinic.PetType

class ClinicBindingInitializer extends WebBindingInitializer {
  
  private var clinic:Clinic = null
  
  @Autowired
  def setClinic(clinic:Clinic) = this.clinic = clinic

  def initBinder(binder:WebDataBinder, request:WebRequest): Unit = {
    val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
    dateFormat.setLenient(false)
    binder.registerCustomEditor(classOf[Date], new CustomDateEditor(dateFormat, false))
    binder.registerCustomEditor(classOf[String], new StringTrimmerEditor(false))
    binder.registerCustomEditor(classOf[PetType], new PetTypeEditor(this.clinic))
  }
}
